<?php
/**
 * 成交率计算
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/1/8
 * Time: 15:23
 */

namespace app\api\service;


use app\api\model\UserWorkuser;

class Rate
{
    public $rate='';
    function __construct($base_rate)
    {
        $this->rate = $base_rate;
    }

    /**
     * 等级一成交率计算
     * @param $times
     * @return float|int
     */
    public function firstLevel($times){
        if ($times == 1){
            $rate = $this->rate;
        }elseif ($times >1){
            $rate = 1;
        }
        return $rate;
    }

    /**
     * 等级二成交率计算
     * @param $times
     * @return float|int
     */
    public function secondLevel($times){
        if ($times == 1){
            $rate = $this->rate;
        }elseif ($times >1){
            $rate = 1;
        }
        return $rate;
    }

    /**
     * 等级三成交率计算
     * @param $times
     * @return float|int
     */
    public function thirdLevel($times){
       $rate = 1;
        return $rate;
    }

    /**
     * 等级四成交率计算
     * @param $times
     * @return float|int
     */
    public function fourthLevel($times){
        $rate = 0.5;
        return $rate;
    }

    /**
     * 等级五成交率计算
     * @param $times
     * @return mixed|string
     */
    public function fifthLevel($times){
        $rate = $this->rate;
        return $rate;
    }

    /**
     * 对我感兴趣成交率
     * @param UserWorkuser $userWorkuser
     * @param $rate
     * @param float $maxRate
     * @return float|string
     */
    public function maxUserRate(UserWorkuser $userWorkuser,$rate,$maxRate=20.00){
        if ($userWorkuser->user_rate < $maxRate){
            $data = bcadd($rate,$userWorkuser->user_rate,2);
            return $data;
        }
        return $maxRate;
    }
    /**
     * 对产品感兴趣分项成交率计算
     * @param UserWorkuser $userWorkuser
     * @param $rate
     * @param $maxRate
     * @return string
     */
    public function maxProductRate(UserWorkuser $userWorkuser,$rate,$maxRate=55.00){
        if ($userWorkuser->product_rate < $maxRate){
            $data = bcadd($rate,$userWorkuser->product_rate,2);
            return $data;
        }
        return $maxRate;
    }
    /**
     * 对公司感兴趣分项成交率计算
     * @param UserWorkuser $userWorkuser
     * @param $rate
     * @param $maxRate
     * @return string
     */
    public function maxWebRate(UserWorkuser $userWorkuser,$rate,$maxRate=5.00){
        if ($userWorkuser->web_rate < $maxRate){
            $data = bcadd($rate,$userWorkuser->web_rate,2);
            return $data;
        }
        return $maxRate;
    }
    /**
     * 成交率计算
     * @param $behavior
     * @param $result
     * @return float|int|mixed|string
     */
    public function getRate($base_rate, $result)
    {
        $rate = new Rate($base_rate);
        $data = '';
        if ($result['weight'] == 1) {
            $data = $rate->firstLevel($result['times']);
        } elseif ($result['weight'] == 2) {
            $data = $rate->secondLevel($result['times']);
        } elseif ($result['weight'] == 3) {
            $data = $rate->thirdLevel($result['times']);
        } elseif ($result['weight'] == 4) {
            $data = $rate->fourthLevel($result['times']);
        } elseif ($result['weight'] == 5) {
            $data = $rate->fifthLevel($result['times']);
        }
        return $data;
    }

}